Subset for Given String
To find all subsets of a set, we can use bitmasking technique. This involves representing each subset as a binary number, where each bit indicates whether a particular element is included in the subset.
- The total number of subsets of a set with N elements is 2^N. This is because each element can either be included or excluded from a subset, resulting in 2N2N combinations.
- By iterating over binary numbers from 0 to 2N−12N−1, you can generate all possible subsets. Each bit in the binary number indicates whether the corresponding element is included in the subset.
package main
import "fmt"
func subset(size int, chars []rune){
for i:=0;i<(1<<size);i++{
for j:=0;j<size;j++{
if i & (1<<j) != 0{
fmt.Printf("%c ",chars[j])
}
}
fmt.Println()
}
}
func main(){
fmt.Printf("Enter the size of Characters: ")
var size int
fmt.Scanln(&size)
fmt.Printf("Enter the Characters: ")
var chars []rune
for i:=0;i<size;i++{
var ch rune
fmt.Scanf("%c",&ch)
chars = append(chars, ch)
}
fmt.Printf("The Original Characters are: %c", chars)
subset(size, chars)
}
Output:
Enter the size of Characters: 4 3
Enter the Characters: xyz
The Original Characters are: [x y z]
x
y
x y
z
x z
y z
x y z